package slide.azheng.pers.coordinatorlayoutcollapsingtoolbarlayoutnestedscrollview;

import android.graphics.Color;
import android.support.design.widget.AppBarLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener {
    private LinearLayout mLinearLayout_shrink;
    private TextView mTextView_name;
    private TextView mTextView_sex;
    private TextView mTextView_year;
    private int mMaskColor;
    private AppBarLayout appbarLayout_behavior;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        appbarLayout_behavior= findViewById(R.id.appbarLayout_behavior);
        mLinearLayout_shrink = findViewById(R.id.mLinearLayout_shrink);
        mTextView_name = findViewById(R.id.mTextView_name);
        mTextView_sex = findViewById(R.id.mTextView_sex);
        mTextView_year = findViewById(R.id.mTextView_year);

        mMaskColor = getResources().getColor(R.color.colorWrite);
        appbarLayout_behavior.addOnOffsetChangedListener(this);
    }

    //AppBarLayout的监听方法
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        int offset = Math.abs(verticalOffset);
        int total = appBarLayout.getTotalScrollRange(); //偏移量
        int alphaOut = (255 - offset) < 0 ? 0 : 255 - offset;
        int maskColorOut = Color.argb(alphaOut, Color.red(mMaskColor),
                Color.green(mMaskColor), Color.blue(mMaskColor));
        if (offset <= total * 140 / 200) {
            mLinearLayout_shrink.setVisibility(View.VISIBLE);
        } else {
            mLinearLayout_shrink.setVisibility(View.GONE);
        }
        mTextView_name.setTextColor(maskColorOut);
        mTextView_sex.setHintTextColor(maskColorOut);
        mTextView_year.setTextColor(maskColorOut);
    }
}
